package pub.cleangao.codegen.enquiry.oracle;

import org.apache.commons.lang3.StringUtils;
import pub.cleangao.codegen.GenConfigHolder;
import pub.cleangao.codegen.enquiry.ColumnEnquiry;
import pub.cleangao.codegen.enquiry.ColumnInfo;
import pub.cleangao.codegen.enquiry.JavaColumnInfo;
import pub.cleangao.codegen.enquiry.TypeFormatter;
import pub.cleangao.codegen.util.ClassField;
import java.util.Map;
import java.util.Set;




public class OracleColumnEnquiry extends ColumnEnquiry {

    private static final TypeFormatter TYPE_FORMATTER = new OracleTypeFormatter();
    private static final String SHOW_SQL =
            // select  utc.column_name as FIELD,utc.data_type TYPE, utc.data_scale SCALE, utc.data_length MAXLENGTH,  		CASE utc.nullable WHEN 'N' THEN '否' ELSE '是' END 可空,  utc.data_default 默认值,ucc.comments COMMENTS,UTC.table_name 表名,  CASE UTC.COLUMN_NAME  WHEN (select  		col.column_name  		from  		user_constraints con,user_cons_columns col  		where  	con.constraint_name=col.constraint_name and con.constraint_type='P'  		and col.table_name='%s')   THEN 'true' ELSE 'false' END AS KEY  		from  user_tab_columns utc,user_col_comments ucc  		where  utc.table_name = ucc.table_name  and utc.column_name = ucc.column_name  and utc.table_name = '%s'  order by  column_id
            """
            SELECT  atc.COLUMN_NAME FIELD, atc.DATA_TYPE TYPE, atc.DATA_SCALE SCALE, atc.DATA_LENGTH MAXLENGTH,  atc.NULLABLE NULLABLE ,  atc.DATA_DEFAULT 默认值, acc.COMMENTS COMMENTS, atc.TABLE_NAME 表名,  CASE atc.COLUMN_NAME  WHEN  ( SELECT col.column_name FROM all_constraints con 	LEFT JOIN all_cons_columns col ON con.table_name = col.table_name 	AND con.OWNER = col.OWNER AND con.CONSTRAINT_NAME = col.CONSTRAINT_NAME    WHERE con.constraint_type = 'P' 	AND col.table_name = '%s' AND con.OWNER = '%s' AND col.column_name = atc.COLUMN_NAME )  THEN 'true' ELSE 'false' END AS KEY  FROM ALL_TAB_COLUMNS atc  LEFT JOIN ALL_COL_COMMENTS acc  ON acc.TABLE_NAME = atc.TABLE_NAME AND acc.COLUMN_NAME = atc.COLUMN_NAME  AND acc.OWNER = atc.OWNER  WHERE atc.TABLE_NAME = '%s' AND atc.OWNER = '%s'  ORDER BY atc.COLUMN_ID
            """;

    public OracleColumnEnquiry(GenConfigHolder config) {
        super(config);
    }

    @Override
    protected String getColumnInfoSql(String tableName) {
        String owner = this.config.getSchemaName();
        return String.format(SHOW_SQL, tableName, owner, tableName, owner);
    }

    @Override
    protected ColumnInfo buildColumnInfo(Map<String, Object> rowMap) {
        Set<String> columnSet = rowMap.keySet();
        for (String columnInfo : columnSet) {
            rowMap.put(columnInfo.toUpperCase(), rowMap.get(columnInfo));
        }
        ColumnInfo columnInfo = new JavaColumnInfo();
        columnInfo.setColumnName(ClassField.convertString(rowMap.get("FIELD")));
        columnInfo.setIsIdentity(false);
        boolean isPk = "true".equalsIgnoreCase(ClassField.convertString(rowMap.get("KEY")));
        columnInfo.setIsPk(isPk);
        String type = ClassField.convertString(rowMap.get("TYPE"));
        if (StringUtils.containsIgnoreCase(type, "number")) {
            Object scaleCol = rowMap.get("SCALE");
            if (scaleCol == null) {
                scaleCol = 0;
            }
            String scale = String.valueOf(scaleCol);
            type = "0".equals(scale) ? "int" : "decimal";
        }
        columnInfo.setType(TYPE_FORMATTER.format(type));
        columnInfo.setComment(ClassField.convertString(rowMap.get("COMMENTS")));
        String maxLength = ClassField.convertString(rowMap.get("MAXLENGTH"));
        columnInfo.setMaxLength(Integer.valueOf(StringUtils.isEmpty(maxLength) ? "0" : maxLength));
        String scale = ClassField.convertString(rowMap.get("SCALE"));
        columnInfo.setScale(Integer.valueOf(StringUtils.isEmpty(scale) ? "0" : scale));
        String isNullable = ClassField.convertString(rowMap.get("NULLABLE"));
        columnInfo.setIsNullable(!"N".equalsIgnoreCase(isNullable));
        return columnInfo;
    }
}
